#include "ImageIO.h"


//void darkChannel(IplImage* rawImage, IplImage* newImage, int K)
//{
//	for (int i = K; i < rawImage->height-K-2; i++){
//		for (int j = K; j < rawImage->width-K-2; j++){
//			uchar* rawPtr = (uchar*)(rawImage->imageData);
//			uchar min = 255;
//			for ( int k1 = -K; k1 <=K; k1++){
//				for (int k2 = -K; k2 <=K; k2++)	{
//					if (rawPtr[(i+k1)*rawImage->widthStep+(j+k2)] < min)
//					{
//						min = rawPtr[(i+k1)*rawImage->widthStep+(j+k2)];
//					}
//				}
//			}
//			uchar* newPtr = (uchar*)newImage->imageData;
//			newPtr[i*newImage->widthStep+j] = min;
//		}
//	}
//}
//
//void brightChannel(IplImage* rawImage, IplImage* newImage, int K)
//{
//	for (int i = K; i < rawImage->height-K-2; i++){
//		for (int j = K; j < rawImage->width-K-2; j++){
//			uchar* rawPtr = (uchar*)(rawImage->imageData);
//			uchar max = 0;
//			for ( int k1 = -K; k1 <=K; k1++){
//				for (int k2 = -K; k2 <=K; k2++)	{
//					if (rawPtr[(i+k1)*rawImage->widthStep+(j+k2)] > max)
//					{
//						max = rawPtr[(i+k1)*rawImage->widthStep+(j+k2)];
//					}
//				}
//			}
//			uchar* newPtr = (uchar*)newImage->imageData;
//			newPtr[i*newImage->widthStep+j] = max;
//		}
//	}
//}



void removeOuterBlank( IplImage* rawImage )
{
	cvFloodFill(rawImage, cvPoint(1,1), cvScalarAll(255), cvScalarAll(10),cvScalarAll(10),NULL,4,NULL);
}

//Only take value from the green channel
void convertToGray( const IplImage* rawImage, IplImage* newImage )
{
	int height = newImage->height;
	int width = newImage->width;
	int widthStep = newImage->widthStep;
	for (int i = 0; i < height; i++){
		for (int j = 0; j < width; j++){
			{
				uchar* rawPtr = (uchar*)(rawImage->imageData);
				uchar* newPtr = (uchar*)(newImage->imageData);
				newPtr[i*widthStep+j] = rawPtr[i*rawImage->widthStep+j*rawImage->nChannels+1];
			}
		}
	}
}

void enhanceGrayImage( IplImage* rawImage )
{
	int height = rawImage->height;
	int width = rawImage->width;
	int widthStep = rawImage->widthStep;
	for (int i = 0; i < height; i++){
		for (int j = 0; j < width; j++){
			{
				uchar* rawPtr = (uchar*)(rawImage->imageData);
				rawPtr[i*widthStep+j] = (unsigned char)((float(rawPtr[i*widthStep+j])*float(rawPtr[i*widthStep+j])/255));
			}
		}
	}
}



void convertToBinary(const IplImage* rawImage, IplImage* newImage, int threshold )
{
	for (int i = 0; i < rawImage->height; i++){
		for (int j = 0; j < rawImage->width; j++){
			{
				uchar* rawPtr = (uchar*)(rawImage->imageData);
				uchar* newPtr = (uchar*)(newImage->imageData);
				if ( rawPtr[i*rawImage->widthStep+j] < threshold ) { 
					newPtr[i*newImage->widthStep+j] = 255;
				}
				else {
					newPtr[i*newImage->widthStep+j] = 0;
				}
			}
		}
	}
}


void copyImage(IplImage* rawImage, IplImage* newImage, int Value)
{
	int height = newImage->height;
	int width = newImage->width;
	int widthStep = newImage->widthStep;

	int sum = 0;
	for (int i = 0; i < height; i++){
		for (int j = 0; j < width; j++){
			{
				uchar* rawPtr = (uchar*)(rawImage->imageData);
				uchar* newPtr = (uchar*)(newImage->imageData);
				if (rawPtr[i*widthStep+j] == Value)
				{
					newPtr[i*widthStep+j] = 0;
					sum++;
				}
				else
				{
					newPtr[i*widthStep+j] = 255;
				}
			}
		}
	}
	if (sum < 50) {
		cout<<"Too few pixels extracted. Please adjust segmentation threshold and try again."<<endl;
		exit(0);
	}
}

